package com.wk.springbootlearn.common.model;

import com.wk.springbootlearn.pojo.constant.CodeConstant;
import com.wk.springbootlearn.pojo.constant.MsgConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@ApiModel("返回体")
@Getter
public class Response {
    @ApiModelProperty("状态码")
    private int code;
    @ApiModelProperty("是否请求成功")
    private boolean success;
    @ApiModelProperty("描述信息")
    private String msg;
    @ApiModelProperty("返回数据")
    private Object data;
    @ApiModelProperty("错误信息")
    private String err;
    @ApiModelProperty("返回时间")
    private String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

    public Response() {
        this.code = CodeConstant.SUCCESS;
        this.msg = MsgConstant.SUCCESS;
        this.success = true;
    }

    public Response(int code) {
        this.code = code;
        if (this.code == CodeConstant.SUCCESS) {
            this.msg = MsgConstant.SUCCESS;
            this.success = true;
        }
        if (this.code == CodeConstant.SERVER_ERROR) {
            this.msg = MsgConstant.FAIL;
            this.success = false;
        }
    }

    public Response(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        if (this.code == CodeConstant.SUCCESS) this.success = true;
        if (this.code == CodeConstant.SERVER_ERROR) this.success = false;
    }

    public Response(int code, String msg, String err) {
        this.code = code;
        this.msg = msg;
        this.err = err;
        if (this.code == CodeConstant.SUCCESS) this.success = true;
        if (this.code == CodeConstant.SERVER_ERROR) this.success = false;
    }

    public Response setOK() {
        this.code = CodeConstant.SUCCESS;
        this.msg = MsgConstant.SUCCESS;
        this.success = true;
        return this;
    }

    public Response setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Response setCode(int code) {
        this.code = code;
        return this;
    }

    public Response setData(Object data) {
        this.data = data;
        return this;
    }

    public Response setFail() {
        this.code = CodeConstant.SERVER_ERROR;
        this.msg = MsgConstant.FAIL;
        this.success = false;
        return this;
    }

    public Response setErr(String err) {
        this.err = err;
        this.code = CodeConstant.SERVER_ERROR;
        this.msg = MsgConstant.FAIL;
        this.success = false;
        return this;
    }

    public Response setSuceess(Boolean success) {
        this.success = success;
        return this;
    }
}
